

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package mass.heatwave;

import java.io.IOException;
import java.util.List;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.ma2.*;

/**
 *
 * @author brett
 */
public class NetCDFReaderStub {
    
    
    public static void main(String args[]) {
    
        NetcdfFile stubby = null;

        try {
            stubby = NetcdfFile.open("/home/brett/netcdf_files/1980_jan/wrfout_d02_1980-01-08_00:00:00", null);	// Open the file
            List<Variable> stubbyVars = stubby.getVariables();	// Get list of file's variables

            System.out.println("Number of variables in file: " + stubbyVars.size());
            System.out.println();
            
//            System.out.println("getdetailinfo: " + stubby.getDetailInfo());
//            System.out.println("filetypedesc " + stubby.getFileTypeDescription());

            // ~~~~~~~~~~~~~ Read each variables ~~~~~~~~~~~~~ //
            for (int i = 0; i < stubbyVars.size(); i++) {
                
                    Variable curr = stubbyVars.get(i);		// Get the current Variable
//                    System.out.println(curr.getFullName());
                    
                    if( curr.getFullName().equals("T2")) {
                        System.out.print("Index " + i + ": ");

                        ArrayFloat.D3 d3values = (ArrayFloat.D3)curr.read();
//                        System.out.println("XXXXXXXX " + d3values.getSize() + "XXXXXXXXXXXXXXXX");
                        
                       
                        //RangeIterator rngs = d3values.getRangeIterator(rngs);
                        Index indx = d3values.getIndex();
                        
//                        System.out.println(indx.getShape(0) + ", " + indx.getShape(1) + ", " + indx.getShape(2));
                        
                        double totalTemp = 0.0;                        
                                                
                        IndexIterator iter = d3values.getIndexIterator();
                        while (iter.hasNext()) {
                            iter.next();
                            System.out.print(iter.getCurrentCounter()[0] + 
                                    ", " + iter.getCurrentCounter()[1] + ", " + 
                                    iter.getCurrentCounter()[2] + ": " + iter.getFloatCurrent() + " ");
                            totalTemp += (double)iter.getFloatCurrent();
                        }
                        double avgTemp = totalTemp / d3values.getSize();
                        
//                        System.out.println("Average Temperature at this time: " + avgTemp);
                        
                        
                    }
            }
                // ~~~~~~~~~~~~~ Done ~~~~~~~~~~~~~ //		

            } catch (java.io.IOException e) {
                e.printStackTrace();
                return;
            } finally {		// Close out the file no matter what occurs
                if (stubby != null)
                    try {
                            stubby.close();
                    } catch (IOException ioe) {
                            ioe.printStackTrace();
                    }
            }
    }

    
}
